class volatiole_subClass {
	volatile int cnt = 0,cnt2 = 0;
	
	public synchronized void inc() {
		cnt++;
		cnt2++;
	}
	
	public synchronized void printResult() {
		System.err.println("cnt:"+cnt+",cnt2:"+cnt2);
	}

	
}


public class threadVolatile_test extends Thread {
	volatile boolean isStop = false;
	static int n = 0;
	String name;
	// static boolean isStop = false;
	volatiole_subClass volatiole_subClass;

	@Override
	public void run() {
		// printN();
		if (name.equals("threadVolatile_test")) {
			while (true) {
				volatiole_subClass.inc();
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			while (true) {
				volatiole_subClass.printResult();
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public threadVolatile_test(String name,
			volatiole_subClass volatiole_subClass) {
		super();
		this.name = name;
		this.volatiole_subClass = volatiole_subClass;

	}


	public void printN() {
		try {
			int i = 0;
			while (!isStop) {
				System.err.println(this.name + ":" + ++i);
				sleep(1000);
			}
			System.err.println("stopped!");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		/*
		 * int length = 1000; threadVolatile_test[] threadVolatile_test = new
		 * threadVolatile_test[length]; for (int i = 0; i < length; i++) {
		 * threadVolatile_test[i] = new threadVolatile_test(); }
		 * 
		 * for (int i = 0; i < length; i++) { threadVolatile_test[i].start(); }
		 * 
		 * for (int i = 0; i < length; i++) { threadVolatile_test[i].join(); }
		 * System.err.println("end:" + n);
		 */
		volatiole_subClass volatiole_subClass = new volatiole_subClass();
		threadVolatile_test threadVolatile_test = new threadVolatile_test(
				"threadVolatile_test", volatiole_subClass);
		threadVolatile_test threadVolatile_test2 = new threadVolatile_test(
				"threadVolatile_test2", volatiole_subClass);
		threadVolatile_test.start();
		threadVolatile_test2.start();
		sleep(3000);
		// threadVolatile_test2.isStop = true;
	}
}
